package ntd.util;


import cn.hutool.core.util.ObjectUtil;
import ntd.util.fun.ComFun;

public class UtilCatch {

    public static <T> T tryCatch(ComFun.FunOut<T> funOut, ComFun.Fun finalFun) {
        try {
            return funOut.accept();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (ObjectUtil.isNotNull(finalFun)) {
                tryCatchNoRes(finalFun);
            }
        }
        return null;
    }

    public static <T> T tryCatch(ComFun.FunOut<T> funOut) {
        return tryCatch(funOut, null);
    }

    public static void tryCatchNoRes(ComFun.Fun fun) {
        tryCatchNoRes(fun, null);
    }

    public static void tryCatchNoRes(ComFun.Fun fun, ComFun.Fun finalFun) {
        try {
            fun.accept();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            try {
                if (ObjectUtil.isNotNull(finalFun)) {
                    finalFun.accept();
                }
            } catch (Throwable e) {
                e.printStackTrace();
                throw new RuntimeException("程序运行异常...");
            }
        }
    }

}
